package com.fosdata.dpm.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fosdata.dpm.domain.SysXbdev;
import com.fosdata.dpm.domain.SysXbtype;
import com.fosdata.dpm.domain.XbBasicInfo;
import com.fosdata.dpm.domain.XbDevRecoder;
import com.fosdata.dpm.entity.dto.AppSysDevDTO;
import com.fosdata.dpm.services.SysXbtypeServices;
import com.fosdata.dpm.services.mapper.SysOrgMapper;
import com.fosdata.dpm.services.mapper.SysXbdevMapper;
import com.fosdata.dpm.services.mapper.XbBasicInfoMapper;
import com.fosdata.dpm.services.XbBasicInfoServices;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fosdata.dpm.services.mapper.XbDevRecoderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 箱变资料说明 服务实现类
 * </p>
 *
 * @author gyk
 * @since 2025-10-22
 */
@Service
public class XbBasicInfoServicesImpl extends ServiceImpl<XbBasicInfoMapper, XbBasicInfo> implements XbBasicInfoServices {


    @Autowired
    private SysXbdevMapper sysXbdevMapper;

    @Autowired
    private XbBasicInfoMapper basicInfoMapper;

    @Autowired
    private XbDevRecoderMapper recoderMapper;

    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Autowired
    SysXbtypeServices sysXbtypeServices;

    @Override
    public XbBasicInfo getBasicByDevCode(String devCode) {
        try {
            LambdaQueryWrapper<XbBasicInfo> lambdaQuery = new LambdaQueryWrapper<>();
            lambdaQuery.eq(XbBasicInfo::getXbCode,devCode);
            XbBasicInfo xbBasicInfo = this.baseMapper.selectOne(lambdaQuery);
            return xbBasicInfo;
        }catch (Exception e){
            log.error(e.getMessage());
            return null;
        }
    }

    //获取全部箱变及其信息
    @Override
    public List<AppSysDevDTO> getAllDev() {
        try {
            List<AppSysDevDTO> devDTOS = new ArrayList<>();
            //所有箱变
            LambdaQueryWrapper<XbBasicInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(XbBasicInfo::getDelFlag,0).orderByAsc(XbBasicInfo::getXbDim);
            List<XbBasicInfo> list = this.basicInfoMapper.selectList(queryWrapper);
            if(list.size()>0){
                //获取所有设备
                //LambdaQueryWrapper<SysXbdev> bsQueryWapper = new LambdaQueryWrapper<>();
                //bsQueryWapper.eq(SysXbdev::getXbdDelFlag,0);
                //List<SysXbdev> sysXbdevs = sysXbdevMapper.selectList(bsQueryWapper);
                //获取所有资料
                //LambdaQueryWrapper<XbDevRecoder> recoderQueryWrapper = new LambdaQueryWrapper<>();
                //List<XbDevRecoder> recoderList = recoderMapper.selectList(recoderQueryWrapper);

                //遍历所有设备查询信息，组成返回对象，加入集合返回
                //Map<Integer, SysXbdev> devMap = list.stream().collect(Collectors.toMap(SysXbdev::getXbdCode, s->s));

                //遍历设备
                for(XbBasicInfo basicInfo:list){
                    AppSysDevDTO appSysDevDTO = new AppSysDevDTO();
                    basicInfo.setOrgName(sysOrgMapper.selectById(basicInfo.getXbOrg()).getOrgName());
                    basicInfo.setTypeName(sysXbtypeServices.getByVbCode(basicInfo.getXbType()).getXbName());
                    appSysDevDTO.setXbBasicInfo(basicInfo);//组装箱变设备
                    //组装基础信息
                    /*if(basicInfoList.size()>0){
                        for(XbBasicInfo info:basicInfoList){
                            if(sysXbdev.getXbdCode().equals(info.getLineCode())){//code相同
                                appSysDevDTO.setXbBasicInfo(info);
                            }
                        }
                    }*/
                    //组装全部资料
                    /*if(recoderList.size()>0){
                        List<XbDevRecoder> recoders = new ArrayList<>();
                        for (XbDevRecoder recoder:recoderList){
                            if(sysXbdev.getXbdCode().equals(recoder.getDevCode())){//code相同
                                recoders.add(recoder);
                            }
                        }
                        appSysDevDTO.setRecoderList(recoders);
                    }*/
                    devDTOS.add(appSysDevDTO);
                }
            }

            return devDTOS;
        }catch (Exception e){
            log.error(e.getMessage());
            return new ArrayList<>();
        }
    }
}
